package il.technion.cs236369.threadPool;

import java.util.concurrent.ConcurrentLinkedQueue;

/**
 * A queue with a finite number of items
 * 
 * @author Gal Lalouche
 * 
 * @param <T>
 */
class FiniteQueue<T> extends ConcurrentLinkedQueue<T> {
	/**
	 * 
	 */
	private static final long serialVersionUID = 4063699634209504289L;
	private final int	queueSize;

	public FiniteQueue(int queueSize) {
		if (queueSize <= 0) {
			throw new IllegalArgumentException("queueSize must be a positive integer");
		}

		this.queueSize = queueSize;
	}

	/**
	 * Same as the normal add, but will throw an IllegalStateException if the
	 * queue is full
	 * 
	 * @throws IllegalStateException if the queue is full
	 * @see ConcurrentLinkedQueue#add(Object)
	 */
	@Override
	public boolean add(T e) {
		if (size() >= queueSize) {
			throw new IllegalStateException("queue is full");
		}

		return super.add(e);
	}
}
